<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            display: flex;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="list">

    </div>
    <button class="pre">上一页</button>
    <button class="next">下一页</button>
    <span></span>
</body>
</html>
<script src="./promise封装ajax.js"></script>
<script>
    //保存当前网页
    var page=1;

    function render() {
        ajax('get', `https://yantianfeng.com/api/emotion`, { page: page, count: 10 }).then((data)=>{  
            var str = data.emotions.map((item, index) => {
                return `<div> <img src="${item.url}" /> <div> ${item.title} </div> </div>`
            }).join('')
            //渲染表情
            document.querySelector('.list').innerHTML = str;
            document.querySelector('span').innerHTML = `${page}/4`

        });
    }

    render()

    //点击上一页, 获取上一页的表情,渲染表情
    document.querySelector('.pre').onclick = function () {

        if (page > 1) {
            --page
            render() 
        }
        //第一页“上一页按钮禁用”，下一页按钮取消禁用
        document.querySelector('.next').disabled = false
        if (page == 1) { document.querySelector('.pre').disabled = true }
    }

    //点击下一页, 获取下一页的表情,渲染表情
    document.querySelector('.next').onclick = function () {

        if (page < 4) {
            ++page
            render()

            document.querySelector('.pre').disabled = false
            if (page == 4) { document.querySelector('.next').disabled = true }

        }
    }
</script>